package algotithm.leetcode.dichotomy.test875;

/**
 * @author 25068
 * @Date 2021/4/27 7:41
 * @Title 875. 爱吃香蕉的珂珂
 */
public class Solution {

    public static void main(String[] args) {
        int[] piles = {3,6,7,11};
        int h = 8;
        Solution solution = new Solution();
        int res = solution.minEatingSpeed(piles, h);
        System.out.println(res);

    }

    public int minEatingSpeed(int[] piles, int h) {
        int l = 1;
        int r = 1000000000;
        while (l < r) {
            int mid = l + (r - l) / 2;
            if (!minEating(piles, h ,mid)) {
                l = mid + 1;
            } else {
                r = mid;
            }
        }
        return l;
    }

    public boolean minEating(int[] piles,int h ,int k) {
        int time = 0;
        for (int p : piles) {
            // 这里向上取整 相当于(p + (k - 1)) / k = (p - 1) / k + 1
            time += (p - 1) / k + 1;
        }
        return time <= h;
    }

}
